package com.sun.juc.sync;

/**
 * 第一步 创建资源类  在资源类定义方法和属性
 * 第二步
 */
public class SaleTicket {
    public static void main(String[] args) {

        Ticket ticket = new Ticket();
        Thread t1 = new Thread(() -> {

            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        }, "A");
        Thread t2 = new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        }, "B");
        Thread t3 = new Thread(() -> {
            for (int i = 0; i < 30; i++) {
                ticket.sale();
            }
        }, "C");
        t1.start();
        t2.start();
        t3.start();
    }
}

class Ticket {
    //票的数量
    private volatile static  int number = 30;

    public synchronized void sale() {


        System.out.println(Thread.currentThread().getName() + "卖出一张票,剩余" + number-- + "张票");
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }
}
